package com.sumu.demo.strategy;

import com.sumu.demo.entity.Fruit;

import java.math.BigDecimal;
import java.util.Map;

public class DiscountPromotionStrategy implements PromotionStrategy {
    private final String targetFruitName;
    private final BigDecimal discountRate;

    public DiscountPromotionStrategy(String targetFruitName, BigDecimal discountRate) {
        this.targetFruitName = targetFruitName;
        this.discountRate = discountRate;
    }

    public BigDecimal calculateDiscountAmount(Map<Fruit, Integer> shoppingItems) {
        BigDecimal total = BigDecimal.ZERO;
        for (Map.Entry<Fruit, Integer> entry : shoppingItems.entrySet()) {
            Fruit fruit = entry.getKey();
            int weight = entry.getValue();
            if (targetFruitName.equals(fruit.getName())) {
                // 目标水果应用折扣：单价 × 斤数 × 折扣率
                total = total.add(fruit.getPricePerJin().multiply(new BigDecimal(weight)).multiply(discountRate));
            } else {
                // 其他水果按原价：单价 × 斤数
                total = total.add(fruit.getPricePerJin().multiply(new BigDecimal(weight)));
            }
        }
        return total;
    }

    @Override
    public BigDecimal calculatePromotionAmount(BigDecimal originalAmount) {
        throw new UnsupportedOperationException("请使用带购物项参数的calculateDiscountAmount方法");
    }
}